package org.lanqiao.ssm.common.pay.alipay.util;

import java.util.Map;

import org.lanqiao.ssm.common.pay.alipay.core.AlipayClientFactory;
import org.lanqiao.ssm.common.pay.alipay.core.Config;
import org.lanqiao.ssm.common.pay.alipay.core.OrderInfoUtil2_0;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;

/**
 * 
 * @Title: AlipayHelper.java
 * @Package org.lanqiao.ssm.common.pay.alipay.util
 * @Description: (支付宝工具类)
 * @author 刘伟 15818570028@163.com
 * @date 2016年11月3日 上午9:42:09
 * @version V1.0
 */
public class AlipayHelper {

	/**
	 * 
	 * @Title: buildAlipayOrder
	 * @Description: (这构造支付宝订单)
	 * @param bizContent：订单数据（必须包含“out_trade_no”订单号和，订单金额“total_amount”）
	 * @return
	 */
	public static String buildAlipayOrder(JSONObject bizContent) {
		Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(bizContent);
		String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
		String sign = OrderInfoUtil2_0.getSign(params, Config.RSA_RRIVATE_KEY);
		return orderParam + "&" + sign;
	}

	/**
	 * 
	 * @Title: alipayTradeQuery
	 * @Description: (支付订单查询)
	 * @param out_trade_no商户订单号
	 * @return
	 * @throws AlipayApiException
	 */
	public static AlipayTradeQueryResponse alipayTradeQuery(String out_trade_no) throws AlipayApiException {
		AlipayClient alipayClient = AlipayClientFactory.getAlipayClientInstance();
		AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
		JSONObject bizContent = new JSONObject();
		bizContent.put("out_trade_no", out_trade_no);
		request.setBizContent(JSON.toJSONString(bizContent));
		return alipayClient.execute(request);
	}

	/**
	 * 
	 * @Title: alipayRefund
	 * @Description: 支付宝统一收单交易退款接口
	 * @param bizContent
	 *            参数必填项,返回结果集 @see
	 *            https://doc.open.alipay.com/doc2/apiDetail.htm?spm=a219a.7629065.0.0.PlTwKb&apiId=759&docType=4
	 * @return
	 * @throws AlipayApiException
	 */
	public static AlipayTradeRefundResponse alipayRefund(JSONObject bizContent) throws AlipayApiException {
		AlipayClient alipayClient = AlipayClientFactory.getAlipayClientInstance();
		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		request.setBizContent(bizContent.toJSONString());
		AlipayTradeRefundResponse response = alipayClient.execute(request);
		return response;
	}

	/**
	 * 
	 * @Title: alipayRefund
	 * @Description: 支付宝统一收单交易退款查询
	 * @param bizContent
	 *            参数必填项,返回结果集 @see
	 *            https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.RUW4IX&docType=4&apiId=1049
	 * @return
	 * @throws AlipayApiException
	 */
	public static AlipayTradeFastpayRefundQueryResponse alipayRefundQueryRequest(JSONObject bizContent)
			throws AlipayApiException {
		AlipayClient alipayClient = AlipayClientFactory.getAlipayClientInstance();
		AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
		request.setBizContent(bizContent.toJSONString());
		AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
		return response;
	}
}
